import JJRequestAgent from './JJRequestAgent.js';
import JJError from './../bizUtil/JJError.js';
import { JJUtil } from '../util/index.js';

export default class JJBaseRequest {

    JJRequestMethod = {
        Post: "POST",
        Get: "GET",
    }

    // request
    methodType = this.JJRequestMethod.Post;
    parameters = {}; // 请求参数
    multipartFormData = null; //上传数据对象类型为({name: '', filePath: ''})
    
    // response
    responseCode = -1;
    responseData = null; // 返回结果
    responseHeader = null; // 应答头
    error = null;

    networkSuccessCallBack = null;
    networkFailCallBack = null;

    isNeedPostResponseNotification = false

    otherInfo = {}

    // 服务器地址
    getHostUrl() {
        return "";
    }

    // 请求路径
    getPathUrl() {
        return "";
    }

    requestParameter() {
        return {}
    }

    getRequestHeader() {
        const header = {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        }
        return header
    }

    doBeforeStart() {
        return Promise.resolve();
    }

    requestCompleteFilter(response) {
        return Promise.resolve(response);
    }

    requestFailedFilter(error) {
        return Promise.resolve(error);
    }

    filterSuccessCallBackValue(isSuccess, value) {
        if (!isSuccess) {
            return value
        }
        
        let result = value['bizData']
        if (JJUtil.isNotNull(result)) {
            return result
        }

        return value
    }

    filterFailCallBackValue(value) {
        return JJError.networkReuqestFailError
    }

    start(networkSuccessCallBack, networkFailCallBack) {
        this.networkSuccessCallBack = networkSuccessCallBack;
        this.networkFailCallBack = networkFailCallBack;

        const me = this
        this.doBeforeStart().then(() => {
            JJRequestAgent.shareInstance().start(me);
        });
    }

    stop() {
        JJRequestAgent.shareInstance().stop(this);
    }

    buildRequestUrl() {
        let url = this.getHostUrl() + this.getPathUrl();
        return url;
    }

    postResponseNotification(isSuccess, value, error) {
    }

    isAllowBeginUploadFile() {
        return true
    }

    isAllowBeginSendRequest() {
        return true
    }
}
